<?php


namespace app\controller\admin;


use app\model\AdminAppVersion;
use app\util\ReturnCode;
use think\facade\Db;
use think\Response;

class Version extends Base
{

    /**
     * @notes: app版本信息列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author: speed
     */
    public function index(): Response {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $keywords = $this->request->get('keywords', '');

        $obj = AdminAppVersion::where('is_delete', 0);

        if ($keywords) {
            $obj = $obj->whereLike('version', "%{$keywords}%");
        }
        $listObj = $obj->with(['school'])->order(['is_top' => 'DESC', 'create_time' => 'DESC'])
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }

    /**
     * 新增版本
     * @return Response
     */
    public function add(): Response {
        $data = [
            'version'       => input('post.version/s'),
            'ios'           => input('post.ios/d'),
            'ios_link'      => input('post.ios_link/s', ''),
            'android'       => input('post.android/d'),
            'android_link'  => input('post.android_link/s', ''),
            'is_top'        => input('post.is_top/d'),
            'force_update'  => input('post.force_update/d'),
            'content'       => input('post.content/s','','htmlspecialchars'),
            'school_id'        => input('post.school_id/d'),
        ];
        $exists = AdminAppVersion::where([
            'is_delete' => 0,
            'version' => $data['version']
        ])->findOrEmpty();
        if (!$exists->isEmpty()) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, "版本号{$data['version']}已存在");
        }

        Db::startTrans();
//        if ($data['is_top'] == 1) {
//            AdminAppVersion::update(['is_top' => 0], ['is_top' => 1, 'is_delete' => 0]);
//        }

        $res = AdminAppVersion::create($data);
        if ($res === false) {
            Db::rollback();
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        Db::commit();
        return $this->buildSuccess();
    }

    /**
     * @notes: 设置为最新版本
     * @return Response
     * @author: speed
     */
    public function riseTop(): Response {
        $id = $this->request->get('id');

        Db::startTrans();
//        AdminAppVersion::update(['is_top' => 0], ['is_top' => 1, 'is_delete' => 0]);
        $res = AdminAppVersion::update([
            'id'         => $id,
            'is_top' => 1
        ]);
        if ($res === false) {
            Db::rollback();
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }

        Db::commit();
        return $this->buildSuccess();
    }

    /**
     * @notes: 编辑版本
     * @return Response
     * @author: speed
     */
    public function edit(): Response {
        $postData = $this->request->post();
        $data = [
            'version'       => input('post.version/s'),
            'ios'           => input('post.ios/d'),
            'ios_link'      => input('post.ios_link/s', ''),
            'android'       => input('post.android/d'),
            'android_link'  => input('post.android_link/s', ''),
            'is_top'        => input('post.is_top/d'),
            'force_update'  => input('post.force_update/d'),
            'content'       => input('post.content/s','','htmlspecialchars'),
            'school_id'        => input('post.school_id/d'),
        ];

//        if ($data['is_top'] == 1) {
//            AdminAppVersion::update(['is_top' => 0], ['is_top' => 1, 'is_delete' => 0]);
//        }
        Db::startTrans();
        $res = AdminAppVersion::update($data, ['id' => $postData['id']]);
        if ($res === false) {
            Db::rollback();
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }

        Db::commit();
        return $this->buildSuccess();

    }

    /**
     * @notes: 删除版本
     * @return Response
     * @author: speed
     */
    public function del(): Response {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        AdminAppVersion::update(['is_delete' => 1], ['id' => $id]);

        return $this->buildSuccess();
    }
}